using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Sinacor.Service.Infra.AuthorizationService.Service.ServiceContract;
using Sinacor.Infra.Service.Security.Services;
using Sinacor.Infra.Service.Security.Authorization;
using Sinacor.Infra.Common.Exceptions.FaultContracts;
using Sinacor.Infra.Service.ExceptionHandling;
using Sinacor.Service.Infra.AuthorizationService.Service.DataContract.Request;
using Sinacor.Service.Infra.AuthorizationService.Service.DataContract.Response;
using Sinacor.Infra.Service.Security.Authentication.ServiceCredentials;
using System.Security;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using System.Xml;

namespace Sinacor.Service.Infra.AuthorizationService.Service.ServiceImplementation
{
    /// <summary>
    /// Classe de implementacao de contrato de servico Authorization
    /// </summary>
    [SinacorServiceBehavior]
    public class User : ServiceContract.IUser
    {
        #region IUser Members

        public VerifyUserLoginResponse VerifyUserLogin(LoginRequest request)
        {
            ISecurityTokenAuthenticationProvider securityTokenAuthenticationProvider = SecurityTokenAutenticationProviderFactory.RetrieveProvider();

            VerifyUserLoginResponse response = new VerifyUserLoginResponse();
            response.ValidLogin = securityTokenAuthenticationProvider.VerifyUserLogin(request.Login);

            return response;
        }

        public VerifyUserLoginResponse ValidateUserLogin(LoginRequest request)
        {
            ISecurityTokenAuthenticationProvider securityTokenAuthenticationProvider = SecurityTokenAutenticationProviderFactory.RetrieveProvider();

            VerifyUserLoginResponse response = new VerifyUserLoginResponse();
            response.ValidLogin = securityTokenAuthenticationProvider.ValidateUserLogin(request.Login, request.Password);

            return response;
        }

        public ChangePasswordResponse ChangePassword(ChangePasswordRequest request)
        {
            ISecurityTokenAuthenticationProvider securityTokenAuthenticationProvider = SecurityTokenAutenticationProviderFactory.RetrieveProvider();

            ChangePasswordResponse response = new ChangePasswordResponse();
            response.PasswordChanged = securityTokenAuthenticationProvider.ChangePassword(request.Login, request.Password, request.NewPassword);

            return response;
        }

        public VerifyChangePasswordResponse VerifyChangePassword(LoginRequest request)
        {
            ISecurityTokenAuthenticationProvider securityTokenAuthenticationProvider = SecurityTokenAutenticationProviderFactory.RetrieveProvider();

            VerifyChangePasswordResponse response = new VerifyChangePasswordResponse();
            response.ChangePassword = securityTokenAuthenticationProvider.VerifyChangePassword(request.Login);

            return response;
        }

        public VerifyIsBlockedResponse VerifyIsBlocked(LoginRequest request)
        {
            ISecurityTokenAuthenticationProvider securityTokenAuthenticationProvider = SecurityTokenAutenticationProviderFactory.RetrieveProvider();
            VerifyIsBlockedResponse response = new VerifyIsBlockedResponse();
            response.IsBlocked = securityTokenAuthenticationProvider.VerifyIsBlocked(request.Login);
            return response;
        }

        public ResetPasswordResponse ResetPassword(LoginRequest request)
        {
            ISecurityTokenAuthenticationProvider securityTokenAuthenticationProvider = SecurityTokenAutenticationProviderFactory.RetrieveProvider();

            ResetPasswordResponse response = new ResetPasswordResponse();
            response.PasswordReset = securityTokenAuthenticationProvider.ResetPassword(request.Login);

            return response;
        }

        //public Stream DownloadLogPackage(DateTime from, DateTime to)
        //{
        //    string assemblyPath = Path.GetFullPath(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath) + @"..\..\..\..\Tools\ErrorLogCollector\Sinacor.Infra.Tools.ErrorLogCollector.dll");

        //    Assembly assembly = Assembly.LoadFile(assemblyPath);

        //    object serverCollector = assembly.CreateInstance("Sinacor.Infra.Tools.ErrorLogCollector.ServerCollector");

        //    object package = serverCollector.GetType().InvokeMember("Collect", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, serverCollector, new object[] { from, to });

        //    MemoryStream stream = new MemoryStream();

        //    XmlSerializer serializer = new XmlSerializer(package.GetType());
        //    serializer.Serialize(stream, package);

        //    stream.Position = 0;

        //    return stream;
        //}

        #endregion
    }
}
